/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "musicplayingpage.h"
#include <appcommon.h>
#include <servicemanager.h>
#include <QApplication>
#include <QFile>
#include <QDesktopServices>


MusicPlayingPage::MusicPlayingPage(QObject *parent) : Page(parent) {
}

int MusicPlayingPage::init() {
    this->mMusicPlayingPageView = qobject_cast<MusicPlayingPageView *>(this->mPageView);
    if(!mMusicPlayingPageView) {
        return -1;
    }

    mMusicModel = new DbModel(QString("select * from  %1 where music_list_id = %2")
                              .arg(this->property("dbtable").toString())
                              .arg(this->property("music_list_id").toString()));
    if(mMusicModel->count() <= 0) {
        return -1;
    }

    ImageData *imagedata = NULL;
    for(int i=0; i< mMusicModel->count(); i++) {
        imagedata = new ImageData();
        imagedata->setName(mMusicModel->value("name",i).toString());
        _musicList.append(imagedata);
    }
    return 0;
}

int MusicPlayingPage::uninit() {
    if(_musicList.empty() == false) {
        foreach(ImageData * i, _musicList) {
            delete i;
            i = NULL;
        }
        _musicList.clear();
    }
    return 0;
}

int MusicPlayingPage::beforeShow() {
    this->mMusicPlayingPageView->showViewUseQml(this);

    return 0;
}



int MusicPlayingPage::afterShow() {

    return 0;
}



int MusicPlayingPage::onClick(int key) {

    return 0;
}

int MusicPlayingPage::onTouch(const QString &type, const QHash<QString, QString> *args) {

    return 0;
}

QString MusicPlayingPage::getBillImagePath(void) {
    return this->property("bill_image").toString();
}

QString MusicPlayingPage::getDetailImagePath(void){
    return this->property("detail_image").toString();
}



void MusicPlayingPage::onIconClick(QString id) {
    if(id == QString("return")) {
        SERVICE(RouterService)->goBack();
    }
}

void MusicPlayingPage::onPlayButtonClick(int index)
{
    QDesktopServices::openUrl(mMusicModel->value("file_url", index).toString());

}

QDeclarativeListProperty<ImageData> MusicPlayingPage::musicList() {
    return QDeclarativeListProperty<ImageData>(this, &_musicList, &imageDatasPropAppend,
            &imageDatasPropCount, &imageDatasPropAt, &imageDatasPropClear);
}
